package com.zl.learning.collection;

import java.util.*;

public class HashSetSortingDemo {
    public static void main(String[] args) {
        HashSet<String> names = new HashSet<String>();
        names.add("Asker");
        names.add("Crak");
        names.add("Bayliss");
        names.add("Mohna");
        names.add("Dina");
        System.out.println("HashSet before sorting : " + names);
        //将HashSet转换为List，然后使用Collections.sort()进行排序
        List<String> tempList = new ArrayList<String>(names);
        Collections.sort(tempList);
        System.out.println("HashSet element in sorted order : " + tempList);
        // 使用TreeSet对HashSet进行排序
        TreeSet<String> sorted = new TreeSet<String>(Comparator.comparingInt(String::length).thenComparingInt(String::hashCode));
        sorted.addAll(names);
        System.out.println("HashSet sorted using TreeSet : " + sorted);
    }
}
